package com.lcxw.hxzbapi.controller.membercontroller;

import com.lcxw.hxzbapi.entity.pojo.Photo;
import com.lcxw.hxzbapi.entity.requestBean.PhotoDelReq;
import com.lcxw.hxzbapi.entity.requestBean.PhotoUploadReq;
import com.lcxw.hxzbapi.entity.requestBean.RequestParamBasic;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbapi.service.PhotoService;

import java.util.Collections;
import java.util.List;
import javax.annotation.Resource;

import com.lcxw.hxzbapi.service.YwpdMemberService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/user/photo")
public class PhotoController {

    @Resource
    PhotoService photoService;

    @Resource
    MemberRealization memberRealization;
    @Resource
    YwpdMemberService ywpdMemberService;
    /**
     * 上传相册图片接口
     */
    @PostMapping("/upload")
    public Response uploadPhoto(@RequestBody @Validated PhotoUploadReq requestBody) {

        return memberRealization.uploadPhoto(requestBody);

    }

    /**
     * 删除一张相册图片接口
     */
    @PostMapping("/delete")
    public Response deletePhoto(@RequestBody @Validated PhotoDelReq requestBody) {

        return memberRealization.deletePhoto(requestBody);

    }

    /**
     * 获取所有相册图片接口
     */
    @PostMapping("/getAll")
    public Response getAllPhoto(@RequestBody RequestParamBasic requestBody) {

        long userId = Long.parseLong(requestBody.getUserId());

        List<Photo> photos = photoService.selectByUserId(userId);

        //2020/10/15 当把头像中的某个照片设置为头像，展示相册的时候需要把这张照片放在第一个位置。
        String imgUrl= ywpdMemberService.selectImgUrlByuserId(Long.parseLong(requestBody.getUserId()));
        if(null!=imgUrl&&!photos.isEmpty()){
            int index=0;
            for(int i=0;i<photos.size();i++){
                if(imgUrl.equals(photos.get(i).getPhotoUrl())){
                    index=i;
                }
            }
            Collections.swap(photos, index, 0);
        }
        return Response.ok(photos);

    }

}
